const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
 //const baseUrl = "https://wkxcx.pdssysc.com/wkfc"; fangzhic
//  const baseUrl = "http://localhost:8080/wkfc";

 //const baseUrl = "https://demo.zidan666.xyz:8446/wkfc";
 const baseUrl = "https://demo.zidan666.xyz:8444/jundedati";

const pyReq = (url, data, callback) => {
  uni.showLoading({title: '加载中',mark:false});
  uni.request({
    url: baseUrl + url, //url
    method: 'POST', //请求方式
    header: {
      'Content-Type': "application/x-www-form-urlencoded",
      'Cookie': 'SESSION=' + uni.getStorageSync("sessionId"),
      'token': uni.getStorageSync("token")
    },
    data: data,
    success: function (msg) {
      uni.hideLoading();
      let res = msg.data
    //console.log(res);
      if (res.code != "0") {
       
		uni.showToast({
          title: res.msg,
          icon: 'none'
        });
        if (res.code == "1001") {
          uni.navigateTo({
            url: '/pages/my-login/my-login'
          })
          uni.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
        return;
      } else if (res.msg != null) {
        uni.showToast({
          title: res.msg,
          icon: 'none'
        });
        return;
      }
      callback(msg)
    },
    fail: function () {
      uni.showToast({
        title: "网络不给力",
        icon: 'none'
      });
    }
  })
}

const baseUrl2 = "https://demo.zidan666.xyz:15743";

const pyReq2 = (url, data, callback) => {
  let isShow=true;
  setTimeout(function(){
    if(isShow)
      wx.showLoading({title: '加载中',mark:false});
  },1000)
  
  wx.request({
    url: baseUrl2 + url, //url
    method: 'POST', //请求方式
    header: {
      'Content-Type': "application/x-www-form-urlencoded",
      'Cookie': 'SESSION=' + wx.getStorageSync("sessionId"),
      'token': wx.getStorageSync("token")
    },
    data: data,
    success: function (msg) {
      isShow = false;
      wx.hideLoading();
      let res = msg.data;
      // console.log(res);
      if (res.code != "0") {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        if (res.code == "1001") {
          wx.navigateTo({
            url: '/pages/my-login/my-login'
          })
          wx.showToast({
            title: res.msg,
            icon: 'none'
          });
        }
        return;
      } else if (res.msg != null) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
        // return;
      }
      callback(msg.data)
    },
    fail: function () {
      wx.showToast({
        title: "网络不给力",
        icon: 'none'
      });
    }
  })
}


module.exports = {
  formatTime: formatTime,
  pyReq:pyReq,
  pyReq2:pyReq2,
  baseUrl:baseUrl,
}
